import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '仪表盘', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/list',
    name: 'User',
    meta: { title: '用户管理', icon: 'user' },
    children: [
      {
        path: 'list',
        component: () => import('@/views/system/user/index'),
        name: 'UserList',
        meta: { title: '用户列表', icon: 'user' }
      }
      // {
      //   path: 'permission',
      //   component: () => import('@/views/system/permission/index'),
      //   name: 'UserPermission',
      //   meta: { title: '权限管理', icon: 'lock' }
      // }
    ]
  },
  {
    path: '/shipping',
    component: Layout,
    redirect: '/shipping/local',
    name: 'Shipping',
    meta: { title: '发货管理', icon: 'shopping' },
    children: [
      {
        path: 'local',
        component: () => import('@/views/shipping/local'),
        name: 'LocalOrder',
        meta: { title: '本地单', icon: 'el-icon-s-order' }
      },
      {
        path: 'transfer',
        component: () => import('@/views/shipping/transfer'),
        name: 'Transfer',
        meta: { title: '转运单', icon: 'el-icon-truck' }
      },
      {
        path: 'aftersale',
        component: () => import('@/views/shipping/aftersale'),
        name: 'Aftersale',
        meta: { title: '退换货单', icon: 'el-icon-service' }
      },
      {
        path: 'relocation',
        component: () => import('@/views/shipping/relocation'),
        name: 'Relocation',
        meta: { title: '调仓单', icon: 'el-icon-s-operation' }
      },
      {
        path: 'cross',
        component: () => import('@/views/shipping/cross'),
        name: 'Cross',
        meta: { title: '跨仓单', icon: 'el-icon-s-promotion' }
      },
      {
        path: 'return',
        component: () => import('@/views/shipping/return'),
        name: 'Return',
        meta: { title: '包裹回国单', icon: 'el-icon-s-home' }
      },
      {
        path: 'inventory-receive',
        component: () => import('@/views/shipping/inventory-receive'),
        name: 'InventoryReceive',
        meta: { title: '库存收货单', icon: 'el-icon-box' }
      },
      {
        path: 'repair',
        component: () => import('@/views/shipping/repair'),
        name: 'Repair',
        meta: { title: '回国维修单收货', icon: 'el-icon-s-tools' }
      },
      {
        path: 'report',
        component: () => import('@/views/shipping/report'),
        name: 'Report',
        meta: { title: '报表', icon: 'chart' }
      },
      {
        path: 'fee',
        component: () => import('@/views/shipping/fee'),
        name: 'Fee',
        meta: { title: '运费管理', icon: 'el-icon-s-operation' }
      },
      {
        path: 'order-qa',
        component: () => import('@/views/shipping/order-qa'),
        name: 'OrderQA',
        meta: { title: '订单问答', icon: 'el-icon-chat-dot-round' }
      }
    ]
  },
  {
    path: '/opration',
    component: Layout,
    redirect: '/opration/api-statistics',
    name: 'Opration',
    meta: {
      title: '运维管理',
      icon: 'el-icon-s-platform'
    },
    children: [
      {
        path: 'config',
        name: 'Config ',
        component: () => import('@/views/opration/config'),
        meta: { title: '系统配置', icon: 'el-icon-setting' }
      },
      {
        path: 'portscan',
        name: 'PortScan',
        component: () => import('@/views/opration/portscan'),
        meta: { title: '端口扫描', icon: 'el-icon-s-tools' }
      },
      {
        path: 'project',
        name: 'Project',
        component: () => import('@/views/opration/project'),
        meta: { title: '项目管理', icon: 'el-icon-s-tools' }
      },
      {
        path: 'device',
        name: 'Device',
        component: () => import('@/views/opration/device'),
        meta: { title: '设备管理', icon: 'el-icon-s-tools' }
      },
      {
        path: 'ssh',
        name: 'SSH',
        component: () => import('@/views/opration/ssh'),
        meta: { title: 'SSH管理', icon: 'el-icon-s-tools' }
      },
      {
        path: 'api-statistics',
        name: 'ApiStatistics',
        component: () => import('@/views/opration/api-statistics'),
        meta: { title: 'API调用统计', icon: 'el-icon-s-tools' }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  /** when your routing map is too long, you can split it into small modules **/
  // componentsRouter,
  // chartsRouter,
  // nestedRouter,
  // tableRouter,

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
